//
//  NSString+GGJson.m
//  ocdemo
//
//  Created by JIENING ZHANG on 2019/6/11.
//  Copyright © 2019 JIENING ZHANG. All rights reserved.
//

#import "NSString+GGJson.h"
#define EmptyString(aString) (nil == aString || 0 == [aString length])
#define NotEmptyString(aString) (nil != aString && 0 < [aString length])

#define EmptyDicOrArray(aobj) (nil == aobj || 0 == [aobj count])
#define NotEmptyDicOrArray(aobj) (nil != aobj && 0 < [aobj count])

#define NotNilString(aString) (aString ?: @"")

@implementation NSString(GGJson)

- (id)ggJsonObj {
    if(self.length == 0) {
        return nil;
    }
    
    NSError *err;
    
    NSData *jsonData = [self dataUsingEncoding:NSUTF8StringEncoding];
    //NSLog(@"jsonArrData length: %lu", [jsonArrData length]);
    
    id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    
    return jsonObj;
}

// https://blog.csdn.net/zeng_zhiming/article/details/68927728
/**
 将中文字符串转换为拼音格式（带声调）
 @return 返回带声调拼音字符串
 */
- (NSString *)transformToPinyinTone
{
    // 空值判断
    if (EmptyString(self)) {
        return @"";
    }
    // 将字符串转为NSMutableString类型
    NSMutableString *string = [self mutableCopy];
    // 将字符串转换为拼音音调格式
    CFStringTransform((__bridge CFMutableStringRef)string, NULL, kCFStringTransformMandarinLatin, NO);
    // 返回带声调拼音字符串
    return string;
}
 
 
/**
 将中文字符串转换为拼音格式（不带声调）
 @return 返回不带声调拼音字符串
 */
- (NSString *)transformToPinyin
{
    // 空值判断
    if (EmptyString(self)) {
        return @"";
    }
    // 将字符串转为NSMutableString类型
    NSMutableString *string = [self mutableCopy];
    // 将字符串转换为拼音音调格式
    CFStringTransform((__bridge CFMutableStringRef)string, NULL, kCFStringTransformMandarinLatin, NO);
    // 去掉音调符号
    CFStringTransform((__bridge CFMutableStringRef)string, NULL, kCFStringTransformStripDiacritics, NO);
    // 返回不带声调拼音字符串
    return string;
}


// 首汉字，首字母大写
- (NSString *)firstChineseCharPinyinFirstLetter {
    // 空值判断
    if (EmptyString(self)) {
        return @"";
    }
    NSString *firstChineseChar = [self substringToIndex:1];
    NSString *pinyin = [firstChineseChar transformToPinyin];
    if EmptyString(pinyin) {
        return @"";
    }
    NSString *pinyinFirstLetter = [pinyin substringToIndex:1];
    
    return [pinyinFirstLetter uppercaseString];
}
 
/**
 将中字符串转换为拼音首字母
 @return 拼音首字母字符串
 */
- (NSString *)transformToFirstLetter
{
    // 空值判断
    if (EmptyString(self)) {
        return @"";
    }
    // 首字母存储
    NSMutableString *firstLetterStirng = [NSMutableString string];
    // 遍历字符串中的所有字符
    for (NSUInteger i = 0; i < self.length; i++) {
        // 将每个字符截取后进行拼音转换
        NSString *charStr = [self substringWithRange:NSMakeRange(i, 1)];
        NSString *pinyin = [charStr transformToPinyin];
        // 存储转换后的拼音首字母
        [firstLetterStirng appendString:[pinyin substringToIndex:1]];

    }
    // 返回中文首字母字符串
    return [firstLetterStirng uppercaseString];
}

@end
